import Swiper from 'swiper/dist/js/swiper.min'

$(function () {
  // 初始化Swiper
  new Swiper('.swiper-container', {
    // Optional parameters
    direction: 'horizontal',
    loop: true,

    // Auto play
    autoplay: {
      delay: 3000,
    },

    // If we need pagination
    pagination: {
      el: '.swiper-pagination',
    }
  })

  const campaign = 'zero-three'
  const av = 'p012'
  const apiUrl = 'https://gate.97kid.com'
  const productId = window.campaignProduct
  const orderUrl = `${window.location.protocol}//${window.location.host}/wx/1order${Math.ceil(Math.random() * 100)}?pid=${productId}&_c=${campaign}`
  utils.initAnalytics(av)
  analytics.send({
    event: `${av}-open`,
    attr: {
      _c: utils.getUrlParam('_c')
    }
  })
  utils.initWechatShare({
    title: '【久趣英语】0元体验3节1对1北美外教课，仅限500名！',
    desc: '100%北美外教，让4-12岁孩子爱上英语！',
    link: `${window.location.protocol}//${window.location.host}/wx/zero-three?_c=${campaign}`,
    imgUrl: 'https://static-app.97kid.com/site-student/public/mobile/campaign/zeroThree/icon/ic_share.png',
    success: target => {
      analytics.send({
        event: `${av}-share`,
        attr: {target}
      })
    }
  })
  const account = new Account()
  account.forceLogin = false
  let unbuying = true
  account.getUserInfo((err, userInfo) => {
    if (!err && userInfo) {
      account.ajax({
        type: 'GET',
        path: `/s/my/products/${productId}/detail`,
        success: res => {
          console.log(res)
          if (!res.total) {
            unbuying = true
          } else {
            unbuying = false
            $('.apply-now').css('background', 'rgba(153, 159, 168, 0.6)').text('已领取')
          }
        },
        error: utils.error
      })
    }
  })

  let productInfo = null
  getProductInfo()
  setInterval(() => {
    if (productInfo) {
      if (productInfo.buyingStartAt - new Date().getTime() / 1000 > 0) {
        // 活动还没开始
        // console.log('活动还没开始')
        if ($(window).scrollTop() > 400) {
          // 显示底部购买条 隐藏购物车
          $('.buy-cart').hide()
          $('.buy-bottom-bar').show()
        } else {
          // 显示购物车 隐藏底部购买条
          $('.buy-cart').hide()
          $('.buy-bottom-bar').hide()
        }
      } else if (productInfo.totalSoldCount >= productInfo.totalLimit) {
        // 活动名额已满
        // console.log('活动名额已满')
        $('.count-down').hide()
        $('.buy-cart').hide()
        $('.buy-bottom-bar').hide()
        $('.sale-over').show()
      } else {
        // 活动进行中
        // console.log('活动进行中')
        if (unbuying) {
          $('.count-down').hide()
          $('.buy-btn').show()
          $('.count-person').show()
          if ($(window).scrollTop() > 400) {
            // 显示底部购买条 隐藏购物车
            $('.buy-cart').hide()
            $('.buy-bottom-bar').show()
          } else {
            // 显示购物车 隐藏底部购买条
            $('.buy-cart').show()
            $('.buy-bottom-bar').hide()
          }
        } else {
          // 隐藏购物车 隐藏底部购买条
          $('.buy-cart').hide()
          $('.buy-bottom-bar').hide()
        }
      }
    }
  }, 100)
  setInterval(getProductInfo, 600000)

  $('.apply-now, .buy-cart, .buy-btn').off('click').on('click', e => {
    analytics.send({
      event: `${av}-apply-btn-click`
    })
    if (unbuying) window.location.href = orderUrl
  })

  function getProductInfo () {
    $.ajax({
      type: 'GET',
      url: `${apiUrl}/s/products/${productId}`,
      contentType: 'application/json',
      success: res => {
        productInfo = res
        console.log(productInfo)
        const x = productInfo.totalSoldCount
        const y = productInfo.totalLimit
        if (x < 400) {
          $('.progress').css('width', `${100 * x / 500}%`)
          $('.count-person > .text > .red').text(Math.max(0, 500 - x))
          $('.total-num').text(productInfo.totalLimit / 10)
        } else {
          const soldout = Math.round(400 + (x - 400) / (y - 400) * 100);
          $('.progress').css('width', `${soldout / 5}%`)
          $('.count-person > .text > .red').text(Math.max(0, 500 - soldout))
          $('.total-num').text(productInfo.totalLimit / 10)
        }
      },
      error: utils.error
    })
  }
})